package view {
	import mx.controls.TextArea;	
	
	import org.puremvc.as3.multicore.interfaces.IProxy;	
	
	import model.DynamicEntityProxy;	
	
	import org.puremvc.as3.multicore.interfaces.INotification;	
	import org.puremvc.as3.multicore.utilities.startupmanager.model.StartupMonitorProxy;	
	
	import mx.controls.Text;	
	
	import view.components.ResourceStatusConsole;	
	
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;	
	
	/**
	 * @author Darshan Sawardekar
	 */
	public class ResourceStatusConsoleMediator extends Mediator {
		
		static public const NAME:String = "ResourceStatusConsoleMediator";
		
		public function ResourceStatusConsoleMediator(viewComponent:ResourceStatusConsole) {
			super(NAME, viewComponent);
		}
		
		public function get resourceStatusConsole():ResourceStatusConsole {
			return viewComponent as ResourceStatusConsole;			
		}
		
		public function get statusText():TextArea {
			return resourceStatusConsole.statusText as TextArea;
		}
		
		override public function listNotificationInterests():Array {
			return [StartupMonitorProxy.LOADING_PROGRESS,
 	                 StartupMonitorProxy.RETRYING_LOAD_RESOURCE,
				     StartupMonitorProxy.LOAD_RESOURCE_TIMED_OUT,
				     StartupMonitorProxy.LOADING_COMPLETE,
				     StartupMonitorProxy.LOADING_FINISHED_INCOMPLETE,
				     StartupMonitorProxy.CALL_OUT_OF_SYNC_IGNORED,
				     StartupMonitorProxy.WAITING_FOR_MORE_RESOURCES,
				     StartupMonitorProxy.RESOURCE_LOADED];
		}
		
		override public function onRegister():void {
			statusText.text = "";
		}
		
		override public function handleNotification(note:INotification):void {
			var name:String = note.getName();
			var entityProxy:DynamicEntityProxy;
			
			switch (name) {
			
				case StartupMonitorProxy.LOADING_PROGRESS:
					//appendStatus("Loading " + String(note.getBody()) + "%");
					break;
					
				case StartupMonitorProxy.RETRYING_LOAD_RESOURCE:
					entityProxy = calcEntityProxy(note);
					if (entityProxy != null) {
						appendStatus("Retrying resource for " + entityProxy.entityName);
					}
					break;
					
				case StartupMonitorProxy.RESOURCE_LOADED:
					entityProxy = calcEntityProxy(note);
					if (entityProxy != null) {
						appendStatus("Loaded Entity id=" + entityProxy.entity.id);
					}
					break;
					
				case StartupMonitorProxy.LOAD_RESOURCE_TIMED_OUT:
					entityProxy = calcEntityProxy(note);
					if (entityProxy != null) {
						appendStatus("Resource load timed out for " + entityProxy.entityName);
					}
					break;
					
				case StartupMonitorProxy.LOADING_COMPLETE:
					appendStatus("Startup complete, all resources loaded successfully.");
					break;
					
				case StartupMonitorProxy.LOADING_FINISHED_INCOMPLETE:
					appendStatus("Startup incomplete, some resources failed to load.");
					break;
					
				case StartupMonitorProxy.CALL_OUT_OF_SYNC_IGNORED:
					appendStatus("addResource call out of sync.");
					break;
					
				case StartupMonitorProxy.WAITING_FOR_MORE_RESOURCES:
					appendStatus("Waiting for more resources ...");
					break;
				
			}
		}
		
		private function calcEntityProxy(note:INotification):DynamicEntityProxy {
			var entityProxy:DynamicEntityProxy = null;
			var srName:String = note.getBody() as String;
			var entityName:String = srName;
			if (srName.indexOf("_sr") != -1) {
				entityName = srName.substring(0, srName.length - 3);
			}
			var tempProxy:IProxy = facade.retrieveProxy(entityName);
			
			if (tempProxy != null && tempProxy is DynamicEntityProxy) {
				entityProxy = tempProxy as DynamicEntityProxy;
			}
			
			return entityProxy;
		}
		
		private function appendStatus(status:String):void {
			if (statusText.text == "") {
				statusText.text += status;
			} else {
				statusText.text += "\r" + status;
			} 
		}
		
	}
}
